import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:jicaimao_flutter/entity/mymessages_bean.dart';
import 'package:jicaimao_flutter/utils/api_address.dart';

import 'package:jicaimao_flutter/utils/http_utils.dart';
import 'package:jicaimao_flutter/utils/sp_key.dart';
import 'package:jicaimao_flutter/utils/sp_utils.dart';
import 'package:jicaimao_flutter/views/mine/my_messages_details.dart';

class MymessagesPage extends StatefulWidget {
  @override
  _MymessagesPageState createState() => _MymessagesPageState();
}

class _MymessagesPageState extends State<MymessagesPage> {
  String userId;

// List<List<Result>> mData = List();
  List<Result> mData = List();

  @override
  void initState() {
    super.initState();
    print('网络请求');
    userId = SpUtils.getString(SpKey.KEY_USER_ID);
    if (userId.length != 0) {
      // 网络请求数据
      _loadData(userId);
    } else {

    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Color(0xFFF6F5F5),
        appBar: AppBar(
          centerTitle: true,
          backgroundColor: Colors.white,
          leading: IconButton(
            icon: Icon(
              Icons.arrow_back_ios,
              color: Colors.grey,
            ),
            onPressed: () => Navigator.pop(context),
          ),
          title: Text(
            '系统消息',
            style: TextStyle(
              fontSize: 16,
              color: Colors.black,
            ),
          ),
        ),
        body: Container(
          child: Column(
            children: <Widget>[
              _ItemData(context, mData),
            ],
          ),
        ),
      ),
    );
  }

  Widget _ItemData(BuildContext context, List<Result> list) {
    if (mData == null || mData.isEmpty) {
      return Container(
          margin: EdgeInsets.fromLTRB(150.0, 250.0, 150.0, 0.0),
          child: new Column(
            children: <Widget>[
              Image.asset(
                'images/no_data.png',
                width: 80.0,
                height: 80.0,
              ),
              Text('暂无数据')
            ],
          ));
    } else {
      return listViewListTile(list);
    }
  }

  ///listView builder 构建
  Widget listViewListTile(List<Result> list) {
    return ListView.builder(
      //设置滑动方向 Axis.horizontal 水平  默认 Axis.vertical 垂直
      scrollDirection: Axis.vertical,
      //内间距
      padding: EdgeInsets.all(10.0),
      //是否倒序显示 默认正序 false  倒序true
      reverse: false,
      primary: true,
      //确定每一个item的高度 会让item加载更加高效
//        itemExtent: 50.0,
      shrinkWrap: true,
      //item 数量
      itemCount: list.length,
      //滑动类型设置
      physics: new ClampingScrollPhysics(),
      //滑动监听
//        controller ,
      itemBuilder: (context, i) => new InkWell(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 0.0),
                  alignment: Alignment.center,
                  child: new Text(
                    "${list[i].creatTime}",
                    style:
                        new TextStyle(fontSize: 16.0, color: Color(0xFF999999)),
                  ),
                ),
                Container(
                  color: Colors.white,
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.fromLTRB(20.0, 10.0, 2.0, 0.0),
                  margin: EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
                  child: new Text(
                    "${list[i].title}",
                    style:
                        new TextStyle(fontSize: 18.0, color: Color(0xFF333333)),
                  ),
                ),
                Container(
                  alignment: Alignment.centerLeft,
                  color: Colors.white,
                  padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 20.0),
                  child: new Text(
                    "${list[i].content}",
                    style:
                        new TextStyle(fontSize: 16.0, color: Color(0xFF333333)),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ],
            ),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => MymessagesDetails(list[i].creatTime,list[i].title,list[i].content)),
              );
            },
          ),
    );
  }

  /// 网络获取数据
  Future _loadData(String userId) async {
    String url = NetAddress.myMessage;
    var data = {'userId': userId};
    var respone = await HttpUtil().post(url, data: data);
    setState(() {
      MymessagesBean _mymessagesBean = MymessagesBean.fromJson(json.decode(respone));
//      mData.add(_mymessagesBean.result);
      var result = _mymessagesBean.result;
      mData = result;
    });
  }
}
